package com.cjack.frenchexcel.service;

import com.cjack.frenchexcel.config.PrivateConfig;
import com.cjack.frenchexcel.dao.RoleDao;
import com.cjack.frenchexcel.model.RoleModel;
import com.cjack.frenchexcel.table.RoleTable;
import com.cjack.frenchexcel.util.ModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台账户管理
 * Created by root on 10/5/19
 */
@Service
public class RoleService {

    @Autowired
    private RoleDao dao;

    public List<RoleModel> list() {
        return ModelUtils.copyListModel(dao.findByCodeNot(PrivateConfig.SUPER_ADMIN_CODE), RoleModel.class);
    }

    public List<RoleModel> listAll() {
        return ModelUtils.copyListModel(dao.findAll(), RoleModel.class);
    }

    public void del(String ids) {
        List<RoleTable> tables = new ArrayList<>();
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            RoleTable table = new RoleTable();
            table.setId(Long.valueOf(id));
            tables.add(table);
        }
        dao.deleteInBatch(tables);
    }

    public void save(RoleModel model) {
        dao.save(ModelUtils.copySignModel(model, RoleTable.class));
    }

    public RoleTable get(Long roleId) {
        return dao.findOne(roleId);
    }

    public RoleTable getByCode(String code) {
        return dao.getByCode(code).get(0);
    }
}
